package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

public class OperationBlock extends Block {
	private int id;
	private static int next = 1;
	private Point upControlPoint;
	private Point downControlPoint;

	public OperationBlock() {
		this.id = next++;
	}

	@Override
	public void draw(Graphics2D g) {
		Point point = super.getPoint();
		g.setColor(new Color(0.4F,0.8F,0.2F,0.5F));
		g.fillRect(point.x, point.y, 60, 40);
		g.setColor(Color.BLACK);
		g.drawRect(point.x, point.y, 60, 40);
		g.setFont(new Font("DialogInput", Font.PLAIN, 18));
		g.drawString("Y"+id, point.x + 20, point.y + 22);
		
	}
	
	@Override
	public void drawControlPoints(Graphics2D g) {
		drawControlPoint(g, upControlPoint);
		drawControlPoint(g, downControlPoint);
	}
	
	@Override
	 public void drawZ(Graphics2D g) {
	  Point point = super.getPoint();
	  g.setFont(new Font("DialogInput", Font.PLAIN, 14));
	  g.setColor(Color.GRAY);
	  g.drawString("(z"+this.getZId()+")", point.x+15, point.y+37);
	  g.setColor(Color.BLACK);
	 }
	
	private void drawControlPoint(Graphics2D g, Point cPoint){
		g.setColor(Color.RED);
		g.drawLine(cPoint.x-3, cPoint.y-2, cPoint.x+3, cPoint.y+2);// line \
		g.drawLine(cPoint.x-3, cPoint.y+2, cPoint.x+3, cPoint.y-2);// line /
		g.setColor(Color.BLACK);
	}
	
	@Override
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public boolean contains(Point point) {
		Point selfPoint = super.getPoint();
		Rectangle rectangle = new Rectangle(selfPoint.x, selfPoint.y, 60, 40);
		return rectangle.contains(point);
	}

	public Point getUpControlPoint() {
		return upControlPoint;
	}

	public void setUpControlPoint(Point upControlPoint) {
		this.upControlPoint = upControlPoint;
	}

	public Point getDownControlPoint() {
		return downControlPoint;
	}

	public void setDownControlPoint(Point downControlPoint) {
		this.downControlPoint = downControlPoint;
	}

	@Override
	public void setPoint(Point point) {
		super.setPoint(point);
		calculateControlPoints();
	}
	
	private void calculateControlPoints() {
		Point point = super.getPoint();
		setUpControlPoint(new Point(point.x+30,point.y));	
		setDownControlPoint(new Point(point.x+30,point.y+40));	
	}

	@Override
	public void cleanDraw(Graphics2D g) {
		Point point = super.getPoint();
		g.setColor(Color.WHITE);
		g.fillRect(point.x, point.y, 60, 40);
		g.drawRect(point.x, point.y, 60, 40);
		g.setFont(new Font("DialogInput", Font.PLAIN, 18));
		g.drawString("Y"+id, point.x + 20, point.y + 22);
	}
	private boolean isControlPoint(Point point,Point cPoint){
		Rectangle rectangle = new Rectangle(cPoint.x-3, cPoint.y-2,6,4);
		if(rectangle.contains(point)){
			return true;
		}
		return false;
	}
	@Override
	public boolean haveControlPoint(Point point) {
		if(isControlPoint(point, downControlPoint) || isControlPoint(point, upControlPoint)){
			return true;
		}
		return false;
	}

	@Override
	public Point getControlPointByClosePoint(Point close) {
		if(isControlPoint(close, downControlPoint)){
			return downControlPoint;
		}else if(isControlPoint(close, upControlPoint)){
			return upControlPoint;
		}
		return null;
	}

	@Override
	public String toString() {
		return "Y"+this.getId()+" z="+super.getZId();
	}

	@Override
	public boolean isCausePoint(Point point) {
		if(isControlPoint(point, downControlPoint))
			return true;
		else
			return false;
	}

}
